﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using SmartPaginator.Extensions;
using System.Windows.Controls;
using System.Windows.Resources;
using System.Windows.Markup;

namespace SmartPaginator.Elements
{
    public class PageElement2: ContentControl
    {
        #region Constructor

        public PageElement2(PageContentElement content, Size pageSize, Thickness pageMargin)
        {
            Content = content;
            this.Width = pageSize.Width;
            this.Height = pageSize.Height;

            this.HeaderHeight = pageMargin.Top;
            this.HeaderMargin = new Thickness(pageMargin.Left, 0, pageMargin.Right, 0);

            this.FooterHeight = pageMargin.Bottom;
            this.FooterMargin = new Thickness(pageMargin.Left, 0, pageMargin.Right, 0);

            this.ContentMargin = new Thickness(pageMargin.Left, 0, pageMargin.Right, 0);

            this.ContentBox = new Rect(new Size(pageSize.Width - pageMargin.Left - pageMargin.Right, 
                                                pageSize.Height - pageMargin.Top - pageMargin.Bottom));

            this.Template = PageElement2.DefaultTemplate;
        }

        #endregion

        #region Properties

        public SectionElement Section
        {
            get 
            {
                var content = Content as PageContentElement;
                if (content == null)
                    return null;

                return content.Section;
            }
        }

        public DataTemplate HeaderTemplate
        {
            get 
            {
                if ((Section == null) || (Section.Source == null))
                    return null;

                return SectionExtensions.GetHeaderTemplate(Section.Source);
            }
        }

        public DataTemplate FooterTemplate
        {
            get
            {
                if ((Section == null) || (Section.Source == null))
                    return null;

                return SectionExtensions.GetFooterTemplate(Section.Source);
            }
        }

        public double HeaderHeight { get; private set; }

        public double FooterHeight { get; private set; }

        public Thickness HeaderMargin { get; private set; }

        public Thickness FooterMargin { get; private set; }

        public Thickness ContentMargin { get; private set; }

        public Rect ContentBox { get; private set; }

        public bool ShowHeader { get; set; }

        public bool ShowFooter { get; set; }

        #endregion

        #region Render Methods

        public void Render()
        {
            //var content = Content as PageContentElement;
            //if (content != null)
            //{
            //    content.Render(ContentBox);
            //}

            Size pageSize = new Size(this.Width, this.Height);
            this.Measure(pageSize);

            Rect pageBounds = new Rect(pageSize);
            this.Arrange(pageBounds);
        }

        #endregion

        #region DefaultTemplate Property

        public static ControlTemplate DefaultTemplate
        {
            get
            {
                if (_DefaultTemplate == null)
                {


                    // Navigate to xaml page
                    Uri uri = new Uri("SmartPaginator;component/Resources/PageTemplate.xaml", UriKind.Relative);
                    StreamResourceInfo info = Application.GetResourceStream(uri);
                    XamlReader reader = new XamlReader();
                    _DefaultTemplate = (ControlTemplate)reader.LoadAsync(info.Stream);
                }
                return _DefaultTemplate;
            }
        }
        private static ControlTemplate _DefaultTemplate;

        #endregion
    }
}
